package com.sc.nft.entity.dto;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sc.nft.entity.DigitalCollectionProductInfoCondition;
import com.sc.nft.entity.DigitalCollectionProductInfoFreeSubscribe;
import com.sc.nft.entity.DigitalCollectionProductInfoSubscribe;
import com.sc.nft.enums.ClassifyType;
import com.sc.nft.enums.CollectionLevel;
import com.sc.nft.enums.CommodityType;
import com.sc.nft.enums.ProductInfoSubscribeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Data
public class ModificationSubscribeProductDTO {
    @ApiModelProperty("productId")
    private Long productId;

    @ApiModelProperty("一级标题")
    @NotBlank(message = "一级标题不能为空")
    private String firstTitle;

    @ApiModelProperty("二级标题")
    private String secondTitle;

    @ApiModelProperty("缩略图")
    @NotBlank(message = "缩略图不能为空")
    private String creationContent;

    @ApiModelProperty("售卖价格")
    private BigDecimal productNum;

    @ApiModelProperty("稀有程度")
    private CollectionLevel level;

    @ApiModelProperty("等级")
    private Integer showLevel;

    @ApiModelProperty("积分价格")
    private BigDecimal integralNum = BigDecimal.valueOf(0.00);

    @ApiModelProperty("藏品/盲盒Id")
    private Long relationId;

    @ApiModelProperty("藏品数量（盲盒不需要）")
    private Integer productCount;

    @ApiModelProperty("作品故事")
    private String productPresentation;

    @ApiModelProperty("权益介绍")
    private String privilegePresentation;

    @ApiModelProperty("开始时间")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @NotNull(message = "开始时间不能为空")
    private Date beginTime;

    @ApiModelProperty("结束时间")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @NotNull(message = "结束时间不能为空")
    private Date endTime;

    @ApiModelProperty("系列id")
    private Long catenaId;

    @ApiModelProperty("系列名称")
    private String catenaName;

    @ApiModelProperty("详情页图类型 1 图片 2:动图 3:3d模型")
    private Integer productDetailPictureType;

    @ApiModelProperty("详情页图")
    private String productDetailPicture;

    @ApiModelProperty("商品类型 ")
    private CommodityType commodityType;

    @ApiModelProperty("分类")
    private ClassifyType typeClassify;

    @ApiModelProperty("购买上限")
    private Integer upperLimit;

    @ApiModelProperty("权益道具id")
    @NotNull(message = "权益道具id不能为空")
    private Long equityPropsId;

//    @ApiModelProperty("权益道具数量")
//    private Integer equityPropsNum;

    @ApiModelProperty("铸造须知")
    private String ecologyCastPresentation;


    @ApiModelProperty("是否为仅熔炼")
    private Boolean isSmeltingOnly = Boolean.FALSE;


    @ApiModelProperty("分润模板Id")
    private Long shareProfitPanelId;

    @ApiModelProperty("是否锁仓")
    @NotNull(message = "请设置是否锁仓")
    private Boolean isLockedPosition;

    @ApiModelProperty("锁仓类型 0:省缺 1:时间模式 2:日期形式 时间模式是获取藏品后延后多久,日期形式是统一锁到对应的时间")
    private Integer lockedPositionType;

    @ApiModelProperty("锁仓市场小时")
    private Integer lockedPositionHour;

    @ApiModelProperty("锁仓到期日期")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lockedPositionTime;

    @ApiModelProperty("是否为平台展示")
    private Boolean platformShow = true;

    @ApiModelProperty("是否为品牌岛展示")
    private Boolean brandShow = false;

    @ApiModelProperty("是否为企业展示")
    private Boolean firmShow = false;

//    @ApiModelProperty("是否开启注册时间限制")
//    @NotNull(message = "是否开启注册时间限制不能为空")
//    private Boolean isRegisterTime;
//
//    @ApiModelProperty("注册开始时间")
//    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
//    private Date registerBeginTime;
//
//    @ApiModelProperty("注册结束时间")
//    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
//    private Date registerEndTime;

    @ApiModelProperty(value = "申购开始时间")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @NotNull(message = "申购开始时间不能为空")
    private Date subscribeStartTime;

    @ApiModelProperty(value = "申购结束时间")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @NotNull(message = "申购结束时间不能为空")
    private Date subscribeEndTime;

    @ApiModelProperty(value = "抽签开始时间")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @NotNull(message = "抽签开始时间不能为空")
    private Date consistenceStartTime;

    @ApiModelProperty(value = "抽签结束时间")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @NotNull(message = "抽签结束时间不能为空")
    private Date consistenceEndTime;

    @ApiModelProperty(value = "最大申购码数量")
    @Min(value = 1, message = "最大申购码数量不能小于1")
    @NotNull(message = "最大申购码数量不能为空")
    private Integer maxSubscribeNum;

    @ApiModelProperty(value = "所需凭证数量")
    @Min(value = 1, message = "所需凭证数量不能小于1")
    @NotNull(message = "所需凭证数量不能为空")
    private Integer voucherSum;

    @ApiModelProperty(value = "兑换的次数")
    @Min(value = 1, message = "兑换次数最小为1")
    @NotNull(message = "兑换次数不能为空")
    private Integer subscribeSum;

    @ApiModelProperty(value = "最大的中签数量")
    @Min(value = 1, message = "最大的中签数量不能小于1")
    @NotNull(message = "最大的中签数量不能为空")
    private Integer maxSubscribe;

    @ApiModelProperty(value = "规则展示图")
    @NotBlank(message = "规则展示图不能为空")
    private String ruleImg;

    @ApiModelProperty(value = "是否有免费申购码配置")
    @NotNull(message = "是否有免费申购码配置不能为空")
    private Boolean freeSubscribe;

    @ApiModelProperty(value = "条件类型: 1全部满足 2满足之一")
    private ProductInfoSubscribeEnum conditionType;

    @ApiModelProperty(value = "新增的时候使用的条件表")
    private List<DigitalCollectionProductInfoConditionDTO> addCondition;

    @ApiModelProperty(value = "免费的申购码领取")
    private List<DigitalCollectionProductInfoFreeSubscribeDTO> addFreeSubscribes;

    @ApiModelProperty("排序")
    private Long collectionsConcernedNum;

    @JsonIgnore
    @ApiModelProperty(value = "申购基础数据")
    private DigitalCollectionProductInfoSubscribe digitalCollectionProductInfoSubscribe = new DigitalCollectionProductInfoSubscribe();

    @ApiModelProperty(value = "申购条件")
    @JsonIgnore
    private List<DigitalCollectionProductInfoCondition> digitalCollectionProductInfoConditions = new ArrayList<>();

    @ApiModelProperty(value = "免费申购码配置.")
    @JsonIgnore
    private List<DigitalCollectionProductInfoFreeSubscribe> digitalCollectionProductInfoFreeSubscribes = new ArrayList<>();

    /**
     * 申购基础数据组装.
     */
    public void basicsDataAssemble(Long productId) {
        digitalCollectionProductInfoSubscribe.setProductId(productId);
        digitalCollectionProductInfoSubscribe.setSubscribeStartTime(this.subscribeStartTime);
        digitalCollectionProductInfoSubscribe.setSubscribeEndTime(this.subscribeEndTime);
        digitalCollectionProductInfoSubscribe.setConsistenceStartTime(this.consistenceStartTime);
        digitalCollectionProductInfoSubscribe.setConsistenceEndTime(this.consistenceEndTime);
        digitalCollectionProductInfoSubscribe.setMaxSubscribeNum(this.maxSubscribeNum);
        digitalCollectionProductInfoSubscribe.setVoucherSum(this.voucherSum);
        digitalCollectionProductInfoSubscribe.setSubscribeSum(this.subscribeSum);
        digitalCollectionProductInfoSubscribe.setMaxSubscribe(this.maxSubscribe);
        digitalCollectionProductInfoSubscribe.setRuleImg(this.ruleImg);
        digitalCollectionProductInfoSubscribe.setPropId(this.equityPropsId);
        digitalCollectionProductInfoSubscribe.setFreeSubscribe(this.freeSubscribe);
        // 这个字段是用来判定当前是否存在参与条件要求 属于冗余字段.如果说前端传入了空的,但是选择了条件类型字段了,也有可能会导致无法判定,不知道数据是否存在,所以直接对他进行承接.
        if (this.conditionType != null) {
            if (CollUtil.isEmpty(this.addCondition)) {
                digitalCollectionProductInfoSubscribe.setParticipationCondition(Boolean.FALSE);
            } else {
                digitalCollectionProductInfoSubscribe.setParticipationCondition(Boolean.TRUE);
            }
        } else {
            digitalCollectionProductInfoSubscribe.setParticipationCondition(Boolean.FALSE);
        }
        digitalCollectionProductInfoSubscribe.setConditionType(this.conditionType);
    }


    /**
     * 申购条件处理,把申购条件组装起来
     */
    public void subscribeCondition(Long productId) {
        if (this.conditionType != null) {
            if (CollUtil.isNotEmpty(this.addCondition)) {
                List<DigitalCollectionProductInfoCondition> data = BeanUtil.copyToList(this.addCondition, DigitalCollectionProductInfoCondition.class);
                for (DigitalCollectionProductInfoCondition result : data) {
                    result.setProductId(productId);
                }
                this.digitalCollectionProductInfoConditions = data;
            }
        }
    }

    /**
     * 免费申购码组装条件
     */
    public void productInfoFreeSubscribes(Long productId) {
        // 勾选了有免费申购码
        if (this.freeSubscribe) {
            if (CollUtil.isNotEmpty(addFreeSubscribes)) {
                List<DigitalCollectionProductInfoFreeSubscribe> data = BeanUtil.copyToList(this.addFreeSubscribes, DigitalCollectionProductInfoFreeSubscribe.class);
                for (DigitalCollectionProductInfoFreeSubscribe result : data) {
                    result.setProductId(productId);
                }
                this.digitalCollectionProductInfoFreeSubscribes = data;
            }
        }
    }


    /**
     * 触发组装基础数据以及申购条件和免费申购码方法组装的钩子.
     *
     * @param productId
     */
    public void trigger(Long productId) {
        basicsDataAssemble(productId);
        subscribeCondition(productId);
        productInfoFreeSubscribes(productId);
    }


    //{
    //	"productId": 1197,
    //	"firstTitle": "小猫",
    //	"secondTitle": "",
    //	"creationContent": "https://cdn.gometa.com.cn/scnft/product/06B5AE4DC70923F5",
    //	"productNum": 123,
    //	"collectionsConcernedNum": 100,
    //	"integralNum": 0,
    //	"productCount": 2,
    //	"productPresentation": "<div style=\"color:#ffffff\"></div>",
    //	"isSoldOut": false,
    //	"alreadyNum": 0,
    //	"laveNum": 2,
    //	"frozenNum": 0,
    //	"digitalCollectionProductInfoUuid": "nft1791379436004098048",
    //	"commodityType": "SUBSCRIBE_COMMODITY",
    //	"typeClassify": "COLLECTION",
    //	"nickName": "玛特DAO",
    //	"status": false,
    //	"productType": 4,
    //	"beginTime": "2024-05-18 16:00:54",
    //	"endTime": "2024-05-18 16:00:56",
    //	"isEnable": false,
    //	"upperLimit": 0,
    //	"priorityPurchaseLimit": 0,
    //	"priorityPurchaseAllLimit": 0,
    //	"priorityPurchaseBeginTime": null,
    //	"priorityPurchaseEndTime": null,
    //	"rushBuyLimit": 0,
    //	"rushBuyBeginTime": null,
    //	"rushBuyEndTime": null,
    //	"isPriorityPurchase": false,
    //	"isRushBuy": false,
    //	"commodityTypeName": "申购藏品",
    //	"equityPropsId": 109,
    //	"equityPropsNum": 3,
    //	"isCanSoldOut": false,
    //	"isLockedPosition": false,
    //	"isSmeltingOnly": false,
    //	"lockedPositionType": 0,
    //	"lockedPositionHour": 0,
    //	"lockedPositionTime": null,
    //	"shareProfitPanelId": 31,
    //	"platformShow": true,
    //	"brandShow": true,
    //	"firmShow": true,
    //	"synthesisCount": 0,
    //	"showSalvagePrefecture": "ROUGH_STONE",
    //	"isRegisterTime": false,
    //	"registerBeginTime": null,
    //	"registerEndTime": null,
    //	"newSort": "",
    //	"showSortInput": false,
    //	"newNumber": "",
    //	"showNewNumberInput": false,
    //	"platforms": "品牌岛、企业H5、玛特宇宙平台",
    //	"catenaName": "创世",
    //	"privilegePresentation": "",
    //	"shareProfitPanelName": "测试",
    //	"subscribeStartTime": "2024-05-17 16:00:49",
    //	"subscribeEndTime": "2024-05-17 18:00:50",
    //	"consistenceStartTime": "2024-05-18 16:00:52",
    //	"consistenceEndTime": "2024-05-18 16:00:53",
    //	"maxSubscribeNum": 2,
    //	"voucherSum": 3,
    //	"subscribeSum": 0,
    //	"maxSubscribe": 2,
    //	"ruleImg": "https://cdn.gometa.com.cn/scnft/product/D20AD174F3AFAC7F",
    //	"propId": 109,
    //	"freeSubscribe": true,
    //	"participationCondition": true,
    //	"conditionType": "ALL",
    //	"digitalCollectionProductInfoConditioAdmins": [{
    //		"collectionId": 62
    //	}, {
    //		"collectionId": 61
    //	}],
    //	"digitalCollectionProductInfoFreeSubscribes": [{
    //		"collectionId": 72,
    //		"quantity": 1
    //	}, {
    //		"collectionId": 73,
    //		"quantity": "2"
    //	}],
    //	"equityFirstTitle": "白名单道具卡",
    //	"equityCreationContent": "https://cdn.gometa.com.cn/DD4CA6C18393DC26",
    //	"addCondition": [62, 61],
    //	"catenaId": 1
    //}
}
